function ParticleSystem()
{
	this.maxParticleCount = 10;
	this.positionX = 0.0;
	this.positionY = 0.0;

	// -- Particle launch values. --

	// The angle the particle is emitted towards
	this.angle = 0;
	
	this.gravity = 0.0;
	this.gravityEnabled = false;

	// The initial color
	this.r = 255;
	this.g = 0;
	this.b = 0;
	
	// The number of active particles
	this.activeCount = 0;
	this.debugCount = 0;
	
	// The collection of particles
	this.particles = new Array(this.maxParticleCount);
	
	this.callBackObject = null;
}

ParticleSystem.prototype =
{
	initialize: function(maxParticles) {
		this.maxParticleCount = maxParticles;
		for (var i = 0; i < this.maxParticleCount; i++)
		{
			this.particles[i] = new Particle();
		}
	},

	registerCallback: function(object) {
		this.callBackObject = object;
	},

	deactivate: function() {
		for (var i = 0; i < this.maxParticleCount; i++)
		{
			this.particles[i].deactivate();
		}
	},

	toggleGravity: function() {
		this.gravityEnabled = !this.gravityEnabled;
	},

	setPos: function(posX, posY) {
		this.positionX = posX;
		this.positionY = posY;
	},

	wrapAngle: function(angle) {
		if (angle > 359)
			return angle - 359;
		else if (angle < 0)
			return 359 + angle;
		return angle;
	},

	triggerExplosion: function() {
		this.activeCount = this.maxParticleCount;
		for (var i = 0; i < this.maxParticleCount; i++)
		{
			var p = this.particles[i];
			p.setPosition(this.positionX, this.positionY);
			p.life = Math.floor(randomRange(200.0, 2500.0));
			p.angle = Math.floor(randomRange(0.0, 359.0));
			p.time = 0;
			p.colorRatio = p.life / 255.0;
			p.speed = randomRange(1.0, 4.0);
			p.calcInitialVelocity();
			p.activate();
		}
	},

	triggerAParticle: function(beginAngle, range) {
		// Look for a non-active particle.
		for (var i = 0; i < this.maxParticleCount; i++)
		{
			var p = this.particles[i];
			if (!p.isActive())
			{
				p.setPosition(this.positionX, this.positionY);
				p.life = Math.floor(randomRange(200.0, 2500.0));
				p.angle = Math.floor(randomRange(beginAngle, beginAngle + range));
				p.angle = this.wrapAngle(p.angle);
				p.time = 0;
				p.colorRatio = p.life / 255.0;
				p.speed = randomRange(1.5, 6.0);
				p.calcInitialVelocity();
				p.activate();
				this.activeCount++;
				break;
			}
		}
	},

	draw: function() {
		if (this.activeCount == 0)
			return;

		for (var i = 0; i < this.maxParticleCount; i++)
		{
			var p = this.particles[i];
			if (p.isActive())
			{
				p.draw();
			}
		}
	},

	setMaxParticles: function(count) {
		this.maxParticleCount = count;
	},

	update: function(timeDelta) {
		if (this.activeCount == 0)
			return false;

		var active;
		for (var i = 0; i < this.maxParticleCount; i++)
		{
			var p = this.particles[i];
			if (p.isActive())
			{
				if (this.gravityEnabled)
					p.updateVelocity(0.0, this.gravity);
				active = p.update(timeDelta);
				if (!active)
				{
					this.activeCount--;
					if (this.activeCount < 0)
						this.activeCount = 0;
				}
				else
				{
					this.callBackObject.collisionCheck(p);
				}
			}
		}

		if (this.activeCount == 0)
			return false;
		else
			return true;
	}
};
